package com.edu.exam.entity.assessment.standard;

import com.edu.exam.enums.assessment.AssessmentMethodCategory;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 考核方式实体类
 * 所属模块：F-07 基础数据管理模块
 * 对应数据库表：assessment_methods
 *
 * 管理课程考核的方式和标准，包含考核方式的基本信息、适用范围、评分标准。
 * 支持考核方式的验证和应用场景判断。
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "assessment_methods")
@Comment("考核方式表")
public class AssessmentMethod {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 考核方式代码，唯一标识符
     */
    @NotBlank(message = "考核方式代码不能为空")
    @Size(max = 50, message = "考核方式代码长度不能超过50个字符")
    @Column(name = "method_code", nullable = false, unique = true, length = 50)
    private String methodCode;

    /**
     * 考核方式名称
     */
    @NotBlank(message = "考核方式名称不能为空")
    @Size(max = 100, message = "考核方式名称长度不能超过100个字符")
    @Column(name = "method_name", nullable = false, length = 100)
    private String methodName;

    /**
     * 考核方式类别
     */
    @NotNull(message = "考核方式类别不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "method_category", nullable = false, length = 50)
    private AssessmentMethodCategory methodCategory;

    /**
     * 描述信息
     */
    @Size(max = 1000, message = "描述信息长度不能超过1000个字符")
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /**
     * 适用范围
     */
    @Size(max = 200, message = "适用范围长度不能超过200个字符")
    @Column(name = "applicable_scope", length = 200)
    private String applicableScope;

    /**
     * 最高分
     */
    @Column(name = "max_score")
    private Integer maxScore;

    /**
     * 及格分
     */
    @Column(name = "passing_score")
    private Integer passingScore;

    /**
     * 权重
     */
    @Column(name = "weight", precision = 5, scale = 2)
    @Builder.Default
    private BigDecimal weight = BigDecimal.valueOf(1.00);

    /**
     * 是否默认方式
     */
    @Column(name = "is_default")
    @Builder.Default
    private Boolean isDefault = false;

    /**
     * 是否系统预定义
     */
    @Column(name = "is_system")
    @Builder.Default
    private Boolean isSystem = false;

    /**
     * 使用次数
     */
    @Column(name = "usage_count")
    @Builder.Default
    private Long usageCount = 0L;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 验证考核方式代码
     */
    public boolean validateMethodCode() {
        return methodCode != null && !methodCode.trim().isEmpty();
    }

    /**
     * 验证分数范围
     */
    public boolean validateScoreRange() {
        if (maxScore == null || passingScore == null) {
            return true;
        }
        return maxScore >= passingScore && passingScore >= 0;
    }

    /**
     * 判断是否适用于给定场景
     */
    public boolean isApplicableFor(String scope) {
        return applicableScope != null && scope != null
                && applicableScope.toLowerCase().contains(scope.toLowerCase());
    }

    /**
     * 获取分数范围描述
     */
    public String getScoreRange() {
        if (maxScore == null) {
            return "无限制";
        }
        return passingScore != null ? passingScore + "-" + maxScore + "分" : "0-" + maxScore + "分";
    }
}